let app = getApp();
import { getGold, getGoldShop } from "../../../request/api.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        currentIndex: 0,
        shopName_ug: 'ئادىل ئالتۇن دۇككىنىدىكى باھا',
        shopName_zh: '公正黄金店黄金价格',
        reminder_ug: 'نۇسقا ھەققىي قوشۇلمىغان باھا',
        reminder_zh: '不计加工费价格',
        goldPrice: [],
        goldShop: [],

        // 分页
        pricePageNo: 1,
        priceTotalPage: 0,
        shopPageNo: 1,
        shopTotalPage: 0
    },

    // My Function

    // 获取内容
    getPost() {
        let data = { pageNo : this.data.pricePageNo }
        getGold(res=>{
            let request = res.data.data
            if (res.statusCode == 200) {
                this.setData({ goldPrice: this.data.goldPrice.concat(request), priceTotalPage : res.data.totalPage })
            }
        }, data);
    },

    // 获取店铺
    getGoldShop() {
        let data = {pageNo : this.data.shopPageNo}
        getGoldShop(res=>{
            let request = res.data.data
            if (res.statusCode == 200) {
                this.setData({ goldShop: this.data.goldShop.concat(request), shopTotalPage : res.data.totalPage })
            }
        }, data);
    },

    // 点击产品
    onClickProduct(item) {
        let shop_id = item.currentTarget.dataset.item.id
        wx.navigateTo({
            url: '/pages/home/gold/goldProduct/goldProduct?shop_id=' + shop_id
        });
    },

    //用户点击bottomTab时调用
    titleClick: function (e) {
        let index = e.currentTarget.dataset.idx
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        })
        this.setData({ currentIndex: e.currentTarget.dataset.idx, pricePageNo: 1, shopPageNo: 1, goldPrice: [], goldShop: [] })
        switch (index) {
            case '0':
                this.getPost()
                break;
            case '1':
                this.getGoldShop()
                break;
            default:
                break;
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "goldPrice", navigation: true })
        this.getPost()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

        if(this.data.currentIndex == 0) {
            if(this.data.pricePageNo == this.data.priceTotalPage) return
            let pageNo = this.data.pricePageNo += 1
            this.setData({pricePageNo : pageNo})
            this.getPost();
        }else if (this.data.currentIndex == 1) {
            if(this.data.shopPageNo == this.data.shopTotalPage) return
            let pageNo = this.data.shopPageNo += 1
            this.setData({shopPageNo : pageNo})
            this.getGoldShop();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})